package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.List;
import java.util.stream.Collectors;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.form.FormProcessInstanceTaskRelVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FormProcessInstanceTaskRelRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowFormProcessInstanceTaskRel;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowFormProcessInstanceTaskRelExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowFormProcessInstanceTaskRelDAO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lilh
 * @date 2020/12/10 下午2:25
 */
@Repository
public class FormProcessInstanceTaskRelRepositoryImpl implements FormProcessInstanceTaskRelRepository {

    @Autowired
    private FlowFormProcessInstanceTaskRelDAO formProcessInstanceTaskRelDAO;

    @Override
    public int insert(FormProcessInstanceTaskRelVo relVo) {
        FlowFormProcessInstanceTaskRel entity = convert(relVo);
        formProcessInstanceTaskRelDAO.insertSelective(entity);
        relVo.setId(entity.getId());
        return entity.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<FormProcessInstanceTaskRelVo> list) {
        for (FormProcessInstanceTaskRelVo item : list) {
            this.insert(item);
        }
    }

    @Override
    public List<FormProcessInstanceTaskRelVo> getStartFormInfo(String processInstanceId) {
        FlowFormProcessInstanceTaskRelExample query = new FlowFormProcessInstanceTaskRelExample();
        query.createCriteria().andProcessInstanceIdEqualTo(processInstanceId).andTaskIdIsNull();
        List<FlowFormProcessInstanceTaskRel> result = formProcessInstanceTaskRelDAO.selectByExampleWithBLOBs(query);
        return result.stream().map(this::convert).collect(Collectors.toList());
    }

    @Override
    public List<FormProcessInstanceTaskRelVo> getTaskFormInfo(String taskId) {
        FlowFormProcessInstanceTaskRelExample query = new FlowFormProcessInstanceTaskRelExample();
        query.createCriteria().andTaskIdEqualTo(taskId);
        List<FlowFormProcessInstanceTaskRel> result = formProcessInstanceTaskRelDAO.selectByExampleWithBLOBs(query);
        return result.stream().map(this::convert).collect(Collectors.toList());
    }

    private FlowFormProcessInstanceTaskRel convert(FormProcessInstanceTaskRelVo entity) {
        FlowFormProcessInstanceTaskRel result = new FlowFormProcessInstanceTaskRel();
        BeanUtils.copyProperties(entity, result);
        return result;
    }

    private FormProcessInstanceTaskRelVo convert(FlowFormProcessInstanceTaskRel entity) {
        FormProcessInstanceTaskRelVo result = new FormProcessInstanceTaskRelVo();
        BeanUtils.copyProperties(entity, result);
        return result;
    }
}
